#include "messageSinglePubKey.h"
#include "networkessential.h"
#include "common.h"
#include <iostream>
using namespace std;

messageSinglePubKey::messageSinglePubKey(const std::string &groupname, 
			c_asgka_element content, int type)
:messageBase(type),
 m_groupname(groupname)
{
	asgka_malloc(m_content);
	strcpy_s(m_content, LENGTH, content);
}

messageSinglePubKey::~messageSinglePubKey()
{
	asgka_free(m_content);
}

void messageSinglePubKey::generateMessage(std::string &buf) const
{
	// Message content :
	// messagetype message_len groupname_len groupname content_len content
	unsigned int messagetype;
	int message_len;
	int groupname_len = m_groupname.size();
	int content_len   = LENGTH;

	messagetype = m_messagetype;
	message_len = groupname_len + content_len + 2 * IntLength;

	// FourBytesToNetChar may change the value of m_type
	buf.append(FourBytesToNetChar(messagetype), IntLength);
	buf.append(FourBytesToNetChar(message_len), IntLength);
	buf.append(FourBytesToNetChar(groupname_len), IntLength);
	buf.append(m_groupname);
	buf.append(FourBytesToNetChar(content_len), IntLength);
	buf.append(m_content, LENGTH);
}
